
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import DBM.DBManager;
import bean.CharacterBEAN;

public class CharacterDAO {
	private final String	addCharacter		= "INSERT INTO CharacterO VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private final String	setAdmin			= "UPDATE CharacterO SET administrator = ? WHERE characterID = ?";
	private final String	getCharacterById	= "SELECT * FROM CharacterO WHERE characterID = ?";
	private final String	setXp				= "UPDATE CharacterO SET experience = ? WHERE characterID = ?";
	private final String	setCashMoney		= "UPDATE CharacterO SET moneyCash = ? WHERE characterID = ?";
	private final String	changeLevel			= "UPDATE CharacterO SET level = ? WHERE characterID = ?";
	private final String	addShootSkill		= "UPDATE CharacterO SET shootExperience = ? WHERE characterID = ?";
	private final String	getCharIdByName		= "SELECT characterID as Id FROM CharacterO WHERE charName = ?";
	private final String	setBankMoney		= "UPDATE CharacterO SET moneyBank = ? WHERE characterID = ?";
	private final String	getCharByName		= "SELECT * FROM CharacterO WHERE charName = ?";
	private final String	updateCharacter		= "UPDATE CharacterO SET experience = ?, moneyCash = ?, moneyBank = ?, shootExperience = ?, respect = ?, vip = ?, administrator = ?  WHERE characterID = ?";
	static DBManager		dbmanager			= null;
	EquipmentDAO			equipmentDAO		= null;
	HomeDAO					homeDAO				= null;
	LandDAO					landDAO				= null;
	ObjectDAO				objectDAO			= null;
	MailDAO					mailDAO				= null;
	NetworkDAO				networkDAO			= null;

	@SuppressWarnings( "static-access" )
	public CharacterDAO( DBManager DBM, EquipmentDAO equipmentDAO,
			HomeDAO homeDAO, MailDAO mailDAO, ObjectDAO objectDAO,
			LandDAO landDAO, NetworkDAO networkDAO ) {
		this.dbmanager = DBM;
		this.equipmentDAO = equipmentDAO;
		this.homeDAO = homeDAO;
		this.landDAO = landDAO;
		this.objectDAO = objectDAO;
		this.mailDAO = mailDAO;
		this.networkDAO = networkDAO;
	}

	public void addCharacter( int newID, String charName ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addCharacter );
			ppstm.setInt( 1, newID );
			ppstm.setInt( 2, 100 );
			ppstm.setString( 3, charName );
			ppstm.setInt( 4, 1 );
			ppstm.setInt( 5, 0 );
			ppstm.setInt( 6, 1000 );
			ppstm.setInt( 7, 50000 );
			ppstm.setInt( 8, newID );
			ppstm.setInt( 9, 5 );
			ppstm.setInt( 10, 0 );
			ppstm.setInt( 11, 0 );
			ppstm.setInt( 12, 0 );
			ppstm.setInt( 13, 0 );
			ppstm.setBoolean( 14, false );
			ppstm.setBoolean( 15, false );
			ppstm.setInt( 16, newID );
			ppstm.setInt( 17, newID );
			ppstm.setInt( 18, 75 );
			ppstm.execute();
			equipmentDAO.addEquipment( newID );
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void setAdmin( int charID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( setAdmin );
			ppstm.setBoolean( 1, true );
			ppstm.setInt( 2, charID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public CharacterBEAN getCharacterByID( int characterID ) {
		CharacterBEAN character = null;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( getCharacterById );
			ppstm.setInt( 1, characterID );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				character = new CharacterBEAN( res.getInt( "characterID" ), res
						.getInt( "health" ), res.getString( "charName" ), res
						.getInt( "experience" ), res.getInt( "level" ), res
						.getInt( "moneyCash" ), res.getInt( "moneyBank" ),
						characterID, res.getInt( "shootExperience" ), res
								.getInt( "respect" ),
						res.getInt( "networkID" ), res.getInt( "landID" ), res
								.getInt( "objectID" ), res.getBoolean( "vip" ),
						res.getBoolean( "administrator" ), res
								.getInt( "homeID" ), ( res.getInt( "mailID" ) ), ( res.getInt( "backfire" ) ) );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return character;
	}

	public void setXp( int xpToSet, int charId ) {
		try {
			PreparedStatement ppstm = dbmanager.makePreparedStatement( setXp );
			ppstm.setInt( 1, xpToSet );
			ppstm.setInt( 2, charId );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void setCashMoney( int cashMoney, int charId ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( setCashMoney );
			ppstm.setInt( 1, cashMoney );
			ppstm.setInt( 2, charId );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void changeLevel( int level, int characterID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( changeLevel );
			ppstm.setInt( 1, level );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void addShootSkill( int characterID, int newShootSkill ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addShootSkill );
			ppstm.setInt( 1, newShootSkill );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public int getCharIDByName( String name ) {
		int id = 0;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( getCharIdByName );
			ppstm.setString( 1, name );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				id = res.getInt( "Id" );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return id;
	}

	public void setBankMoney( int amount, int charId ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( setBankMoney );
			ppstm.setInt( 1, amount );
			ppstm.setInt( 2, charId );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public ArrayList< CharacterBEAN > getCharacterByName( String name,
			boolean startsWith ) {
		ArrayList< CharacterBEAN > list = new ArrayList< CharacterBEAN >();
		try {
			ResultSet res = null;
			if( startsWith ) { // doesnt work, fix this later on. no prior at
								// all
				PreparedStatement ppstm = dbmanager
						.makePreparedStatement( "SELECT * FROM CharacterO WHERE charName LIKE ?" );
				ppstm.setString( 1, name + "%" );
				res = ppstm.executeQuery();
			} else {
				PreparedStatement ppstm = dbmanager
						.makePreparedStatement( getCharByName );
				ppstm.setString( 1, name );
				res = ppstm.executeQuery();
			}
			while( res.next() ) {
				CharacterBEAN character = new CharacterBEAN( res
						.getInt( "characterID" ), res.getInt( "health" ), res
						.getString( "charName" ), res.getInt( "experience" ),
						res.getInt( "level" ), res.getInt( "moneyCash" ), res
								.getInt( "moneyBank" ), res
								.getInt( "characterID" ), res
								.getInt( "shootExperience" ), res
								.getInt( "respect" ),
						res.getInt( "networkID" ), res.getInt( "landID" ), res
								.getInt( "objectID" ), res.getBoolean( "vip" ),
						res.getBoolean( "administrator" ), res
								.getInt( "homeID" ), ( res.getInt( "mailID" ) ), ( res.getInt( "backfire" ) ) );
				list.add( character );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return list;
	}

	public void updateCharacter( int userId, int exp, int moneyCash,
			int moneyBank, int shootExperience, int respect, boolean vip,
			boolean admin ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( updateCharacter );
			ppstm.setInt( 1, exp );
			ppstm.setInt( 2, moneyCash );
			ppstm.setInt( 3, moneyBank );
			ppstm.setInt( 4, shootExperience );
			ppstm.setInt( 5, respect );
			ppstm.setBoolean( 6, vip );
			ppstm.setBoolean( 7, admin );
			ppstm.setInt( 8, userId );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	/*
	 * BAD CODE! D:
	 */
	public static String getNameByID( int charID ) {
		String name = null;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( "SELECT charName FROM CharacterO WHERE characterID = ?" );
			ppstm.setInt( 1, charID );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				name = res.getString( "charName" );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return name;
	}
}
